package com.unicampmaps;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HomePanelAdapter extends BaseAdapter {
	private Context mContext;
	
	private int[] strList = {R.string.home_cantinas, R.string.home_salas_de_aula,
							 R.string.home_servicos, R.string.home_bibliotecas,
							 R.string.home_transporte, R.string.home_outros};

	private int[] iconList = {R.drawable.food, R.drawable.classroom,
				              R.drawable.cog, R.drawable.book, 
				              R.drawable.bus, R.drawable.info};
	
	private int[] colorList = {R.color.panel_cantinas, R.color.panel_salas_de_aula,
					           R.color.panel_servicos, R.color.panel_bibliotecas, 
					           R.color.panel_transporte, R.color.panel_outros};
	
	// Gets the context so it can be used later
	public HomePanelAdapter(Context c) {
		mContext = c;
	}

	// Total number of things contained within the adapter
	@Override
	public int getCount() {
		return strList.length;
	}

	// Require for structure, not really used in my code.
	@Override
	public Object getItem(int position) {
		return null;
	}

	// Require for structure, not really used in my code. Can
	// be used to get the id of an item in the adapter for
	// manual control.
	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater li =  (LayoutInflater)this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View tempView = li.inflate(R.layout.home_item, null);
		ImageView iconMain = (ImageView) tempView.findViewById(R.id.panel_icon);
		TextView textMain = (TextView) tempView.findViewById(R.id.panel_label);
		View panelMain = tempView.findViewById(R.id.panel_top);
		
		int color = this.mContext.getResources().getColor(colorList[position]);
		
		Drawable drawableNotPressed = new ColorDrawable(color);
        Drawable drawable = new ColorDrawable(R.color.panel_pressed);
        
        StateListDrawable listDrawable = new StateListDrawable();
		listDrawable.addState(new int[] {android.R.attr.state_pressed}, drawable);
		listDrawable.addState(new int[] {android.R.attr.state_focused}, drawable);
		listDrawable.addState(new int[] {}, drawableNotPressed);
        
        panelMain.setBackgroundDrawable(listDrawable);
		
		textMain.setText(this.strList[position]);

		iconMain.setVisibility(View.VISIBLE);
		iconMain.setImageResource(iconList[position]);

		return tempView;
	}
}
